import types

from toneedu.exceptions import NoActivePluginError

class paramNum(object):
    """Decorator for checking the number of parameters"""

    def __init__(self, paramNum):
        self._paramNum = paramNum

    def __call__(self, f):
        def wrapper(*args):
            if len(args[1]) != self._paramNum:
                return "Number of parameters not matched: %d given, and %d required. Please use 'help' command for more information" \
                            % (len(args[1]), self._paramNum)
            else:
                return f(*args)

        return wrapper

class paramCheck(object):
    """Decorator for checking the length of parameter-list with a function"""

    def __init__(self, pred):
        self._pred = pred

    def __call__(self, f):
        def wrapper(*args):
            if self._pred(len(args[1])):
                return f(*args)
            else:
                return "Number of parameters not matched. Please use 'help' command for more information"

        return wrapper

class pluginRequired(object):
    """Decorator for checking wheter there's an active plugin or not"""
    
    __get__ = types.MethodType

    def __init__(self, f):
        self._f = f

    def __call__(self, *args):
        if args[0]._plugin is None:
            raise NoActivePluginError
        else:
            return self._f(*args)

